package wiki.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import wiki.banco.Conexao;
import wiki.beans.Defesa;

public class DefesaDAO 
{
	Conexao conexao;
	
	
	public DefesaDAO()
	{
		conexao = new Conexao();
	}
	
	
	protected void finalize()
	{
		conexao.fechar();
	}
	
	
	public boolean inserir(Defesa d)
	{
	    String query = "INSERT INTO defesa (codNoticia, codAtrasador, titulo, conteudo, data, ativo) VALUES (?, ?, ?, ?, ?, ?)";
	    conexao.preparar(query);
	    
	    try
	    {
	    	conexao.getPstmt().setInt(1, d.getCodNoticia());
	    	conexao.getPstmt().setInt(2, d.getIdAtrasador().getIdAtrasador().getIdPessoa());
	    	conexao.getPstmt().setString(3, d.getTitulo());
	    	conexao.getPstmt().setString(4, d.getConteudo());
	    	conexao.getPstmt().setTimestamp(5, new Timestamp(new java.util.Date().getTime()));
	    	conexao.getPstmt().setBoolean(6, d.getAtivo());
	      
	    	return conexao.executeUpdate(); 
	    } 
	    catch (SQLException ex)
	    {
	    	Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	    	return false;
	    }
	}
	
	
	public boolean excluir(Defesa d)
	{
	    String query = "DELETE FROM defesa WHERE id=?";
	    conexao.preparar(query);
	    
	    try
	    {
	      conexao.getPstmt().setInt(1, d.getId());
	      return conexao.executeUpdate();
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	      return false;
	    }
	}
	
	
	public boolean excluir(int id)
	{
	    String query = "DELETE FROM defesa WHERE id=?";
	    conexao.preparar(query);
	    
	    try
	    {
	      conexao.getPstmt().setInt(1, id);
	      return conexao.executeUpdate();
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	      return false;
	    }
	}
	
	
	public boolean excluirLogicamente(int id)
	{
	    String query = "UPDATE defesa SET ativo=false WHERE id=?";
	    conexao.preparar(query);
	    
	    try
	    {
	      conexao.getPstmt().setInt(1, id);
	      return conexao.executeUpdate();
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	      return false;
	    }
	}
	
	
	public boolean atualizar(Defesa antigo, Defesa novo)
	{
	     String query = "UPDATE defesa SET codNoticia=?, codAtrasador=?, titulo=?, conteudo=?, ativo=? " +
	     				"WHERE id=?";
	     conexao.preparar(query);
	    
	     try
	    {
	    	conexao.getPstmt().setInt(1, (novo.getCodNoticia()>0)?novo.getCodNoticia():antigo.getCodNoticia());
	    	conexao.getPstmt().setInt(2, (novo.getIdAtrasador().getIdAtrasador().getIdPessoa()>0)?novo.getIdAtrasador().getIdAtrasador().getIdPessoa():antigo.getIdAtrasador().getIdAtrasador().getIdPessoa());
	    	conexao.getPstmt().setString(3, (novo.getTitulo()!="")?novo.getTitulo():antigo.getTitulo());
	    	conexao.getPstmt().setString(4, (novo.getConteudo()!="")?novo.getConteudo():antigo.getConteudo());
	    	conexao.getPstmt().setBoolean(5, (novo.getAtivo()!=false)?novo.getAtivo():antigo.getAtivo());
	    	
	    	conexao.getPstmt().setInt(6, antigo.getId());
	    	return conexao.executeUpdate();
	    } catch (SQLException ex)
	    {
	      Logger.getLogger(Defesa.class.getName()).log(Level.SEVERE, null, ex);
	      return false;
	    }
	}
	
	
	public ArrayList<Defesa> buscarTodos()
	{
	    ArrayList<Defesa> arrayRegistros = new ArrayList<Defesa>();
	    
	    String query = "SELECT idDefesa, codNoticia, codAtrasador, titulo, conteudo, DATE_FORMAT(data, '%d/%m/%Y %H:%i') data, ativo " +
	    			   "FROM defesa WHERE ativo<>false ORDER BY idDefesa";
	    conexao.preparar(query);
	    
	    try
	    {
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  arrayRegistros.add(new Defesa(rs.getInt("idDefesa"), rs.getInt("codNoticia"), rs.getInt("codAtrasador"), rs.getString("titulo"), rs.getString("conteudo"), rs.getString("data") ,rs.getBoolean("ativo")));
	      }
	      
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
	    return arrayRegistros;
	}
	
	
	@SuppressWarnings("finally")
	public Defesa buscarPorID(int id)
	{
	    Defesa defesa = null;
	    String query = "SELECT idDefesa, codNoticia, codAtrasador, titulo, conteudo, DATE_FORMAT(data, '%d/%m/%Y %H:%i') data, ativo " +
	    			   "FROM defesa WHERE postAtivo<>false AND idDefesa=?";

	    conexao.preparar(query);
	    try
	    {
	      conexao.getPstmt().setInt(1, id);
	      ResultSet rs = conexao.executeQuery();
	      
	      while (rs.next())
	      {
	    	  AtrasadorDAO a = new AtrasadorDAO();
	    	  
	    	  defesa = new Defesa();
	    	  defesa.setId(id);
	    	  defesa.setCodNoticia(rs.getInt("codNoticia"));
	    	  defesa.setIdAtrasador(a.buscar(rs.getInt("codAtrasador")).get(0));
	    	  defesa.setTitulo(rs.getString("titulo"));
	    	  defesa.setConteudo(rs.getString("conteudo"));
	    	  System.out.println("Teste: "+rs.getString("data"));
	    	  defesa.setData(rs.getString("data"));
	    	  defesa.setAtivo(rs.getBoolean("ativo"));
	      }
	      rs.close();
	      
	    } 
	    catch (SQLException ex)
	    {
	      Logger.getLogger(DefesaDAO.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    finally
	    {
	      return defesa;
	    }
	}
}


